package com.demo.wx;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpKefuService;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpKefuServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import net.sf.json.JSONObject;
import util.HttpUtils;
import util.ResultData;
import util.ResultDataUtil;

import com.demo.common.WeixinUtils;
import com.jfinal.core.Controller;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;


public class wxController  extends Controller {
	
	private WxMpInMemoryConfigStorage config;
	final String API_URL = "http://182.92.98.147:7000";
	//final String API_PROID = PropKit.use("config.properties").get(BaseConstant.API_PROID);
	public static String URL_OAUTH2_AUTHORIZE = "https://open.weixin.qq.com/connect/oauth2/authorize";
	
	protected WxMpService wxMpService;
	protected WxMpMessageRouter wxMpMessageRouter;

	/**
	 * 微信服务器验证
	 * @return
	 */
	public void wkyxCheck(){
		 ResultData resultData = new ResultData();
		System.err.println("----------------------------");
		 String code = getPara("code");
		 System.out.println("code========="+code);
		 String state = String.valueOf(System.currentTimeMillis());
		 String access_token = "";
		 String openid = "";
		 String urlCode = "";
		 if(code==null){
			 String redirect_uri = "http://59.110.138.199/demo/wx/wkyxCheck";
			 urlCode = WeixinUtils.getOAuthBase(WeixinUtils.APPID_MARKET,redirect_uri);
		 }else {
			  urlCode = "http://59.110.138.199/demo";
		}
		 System.out.println("urlCode========="+urlCode);
		 redirect(urlCode);
//		 else {
//			 resultData= WeixinUtils.getAccessToken(code,state,WeixinUtils.APPID_MARKET,WeixinUtils.APPSECRET_MARKET);
//			 if(ResultDataUtil.isSuccess(resultData)){
//					JSONObject result =JSONObject.fromObject(resultData.getData());
//					
//					if(!result.isEmpty() && result.containsKey("openid")){
//						openid = result.getString("openid");
//					}
//					System.out.println("openid========="+openid);
//				}
//			 renderNull();
//		}
		 
//		 String token="Token123";
//		 config = new WxMpInMemoryConfigStorage();
//		 config.setToken(token);    // 设置微信公众号的token
//		 config.setAppId("wx1fa8e31b22a0385e");    // 设置微信公众号的appid
//		 config.setSecret("447626355a5bc1212c8f59c3b569956d");  // 设置微信公众号的app corpSecret
//	     wxMpService = new WxMpServiceImpl();
//		 wxMpService.setWxMpConfigStorage(config);
//		 WxMpKefuMessage message = new WxMpKefuMessage();
//		 message.setContent("Hello World");
//		 message.setMsgType("text");
//		 message.setToUser(openid);
//		 WxMpKefuService kefuService = new WxMpKefuServiceImpl(wxMpService);
//		 try {
//			kefuService.sendKefuMessage(message);
//		} catch (WxErrorException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		 
		 
		 
//		 String tokenurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx7b59340d20d8ef77&secret=13ecea5da95e0d4dc4dc1ee9688f639b";
//		 
//		 String res="";
//		try {
//			res = HttpUtils.sendGet2(tokenurl);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		JSONObject result =JSONObject.fromObject(res);
//		access_token = result.getString("access_token");
////		 String res = WeixinUtils.getCode(url);
//		 System.out.println("res======"+res);
//		 
			
		}
	
	
	
	
	
	
	
	 /**
	  *  按钮
	  */
	 public static void main(String[] args) {
        
	        //创建菜单
	        //创建一级菜单
	        WxMenuButton button1=new WxMenuButton();
	        button1.setName("查信用");
	        button1.setType("view");
	        button1.setKey("key1"); 
	        button1.setUrl("http://59.110.138.199/demo/wx/wkyxCheck");
	         
	        List<WxMenuButton> buttons=new ArrayList<WxMenuButton>();
	        buttons.add(button1);
	        
	        WxMenu menu=new WxMenu();
	        menu.setButtons(buttons);
	         
	         
	        //发送请求 创建菜单
	        WxMpService service=new WxMpServiceImpl(); 
	        WxMpInMemoryConfigStorage wxConfigProvider=new WxMpInMemoryConfigStorage();
	        wxConfigProvider.setAppId("wx1fa8e31b22a0385e");
	        wxConfigProvider.setSecret("447626355a5bc1212c8f59c3b569956d");
	        service.setWxMpConfigStorage(wxConfigProvider);
	        try {
	            service.getMenuService().menuCreate(menu);
	        } catch (WxErrorException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	 
	 
	    }

}
